package tree.test257;

import listbyorder.utils.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * @author code_yc
 * @version 1.0
 * @date 2020/7/17 13:02
 */
public class Solution1 {

    List<String> res = new ArrayList<>();

    public List<String> binaryTreePaths(TreeNode root) {
        if (root == null) return res;
        StringBuilder path = new StringBuilder();
        path.append(root.val);
        dfs(root, path);
        return res;
    }

    private void dfs(TreeNode root, StringBuilder path) {
        if (root.left == null && root.right == null) {
            res.add(new String(path));
            return;
        }
        StringBuilder before;
        if (root.left != null) {
            before = new StringBuilder(path);
            before.append("->").append(root.left.val);
            dfs(root.left, before);
        }
        if (root.right != null) {
            before = new StringBuilder(path);
            before.append("->").append(root.right.val);
            dfs(root.right, before);
        }
    }
}
